package com.siyuanren.gcocp.config;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.siyuanren.gcocp.common.config.PublicConfig;
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInRedisConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.api.impl.WxMpServiceJoddHttpImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;

/**
 * 微信公众号主配置
 * <p>
 * Created by bjliumingbo on 2017/5/12.
 *
 * @author FirenzesEagle
 * @author BinaryWang
 */
@Configuration
public class MainConfiguration {
//    @Value("#{appId}")
//    public String appId;
//
//    @Value("#{appSecret}")
//    public String appSecret = "177a542cde192062390ed64fb26cc328";
//
//    @Value("#{token}")
//    public String token = "siyuanren";
//
//    @Value("#{aesKey}")
//    public String aesKey;

    @Autowired
    private JedisPool jedisPool;

    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {
//        WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
        WxMpInMemoryConfigStorage configStorage = new WxMpInRedisConfigStorage(jedisPool);
        configStorage.setAppId(PublicConfig.PUBLIC_WX_APPID);
        configStorage.setSecret(PublicConfig.PUBLIC_WX_APPSECRET);
        configStorage.setToken(PublicConfig.PUBLIC_WX_TOKEN);
        configStorage.setAesKey(PublicConfig.PUBLIC_WX_AESKEY);
        return configStorage;
    }

    @Bean
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceJoddHttpImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }

    @Bean
    public WxPayConfig payConfig() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(PublicConfig.PUBLIC_WX_APPID);
        payConfig.setMchId(PublicConfig.PUBLIC_WX_MCHID);
        payConfig.setMchKey(PublicConfig.PUBLIC_WX_MCHKEY);
        payConfig.setSubAppId(PublicConfig.PUBLIC_WX_SUBAPPID);
        payConfig.setSubMchId(PublicConfig.PUBLIC_WX_SUBMCHID);
        payConfig.setKeyPath(PublicConfig.PUBLIC_WX_KEYPATH);
        return payConfig;
    }

    @Bean
    public WxPayService wxPayService(WxPayConfig payConfig) {
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }
}
